package com.tutu.controller.backend;

import com.tutu.common.DataTableFactory;
import com.tutu.controller.common.CommonController;
import com.tutu.entity.Module;
import com.tutu.service.RoleModuleService;
import com.tutu.service.RoleService;
import com.tutu.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 权限管理
 * Created by 涂奕恒 on 2019/5/27.
 */
@Controller
@RequestMapping(value = "backend/power")
public class PowerController extends CommonController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private RoleModuleService roleModuleService;

    @RequestMapping(value = "/index")
    public String index() {
        return "backend/power/list";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response, Integer draw, Integer start, Integer length) {
        int pageNum = getPageNum(start, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, roleService.pageByParams(pageNum, length));

        WebUtil.printJson(response, result);
    }

    @SuppressWarnings("Duplicates")
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String, Object> delete(HttpServletRequest request, Integer id) {
        return roleService.deleteById(request, id);
    }

    @RequestMapping("/save")
    @ResponseBody
    public Map<String, Object> save(HttpServletRequest request, Integer id, String name) {
        return roleService.saveInfo(request, id, name, "");
    }

    @RequestMapping("/findListByRoleId")
    @ResponseBody
    public List<Module> findListByRoleId(Integer roleId) {
        return roleModuleService.findListByRoleId(roleId);
    }

    @RequestMapping("/savePower")
    @ResponseBody
    public Map<String, Object> savePower(HttpServletRequest request, Integer id, String ids) {
        return roleModuleService.savePower(request, id, ids);
    }
}
